package com.xm.xmapp.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.LongReRentOrderActivity;
import com.xm.xmapp.LongRentOrderCommentActivity;
import com.xm.xmapp.LongRentOrderDetailsActivity;
import com.xm.xmapp.R;
import com.xm.xmapp.adapter.LongRentOrderListAdapter;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.customview.XListView;
import com.xm.xmapp.customview.XListView.IXListViewListener;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.ListViewOnclickInterFace;
import com.xm.xmapp.myinterface.PinJiaInterFace;
import com.xm.xmapp.request.bean.Get_user_order_listRequest;
import com.xm.xmapp.response.bean.ResponseUserOrderList;
import com.xm.xmapp.response.bean.OrderBeanResponse;

public class LongRentOrderFragment extends BaseFragment {
	
	private XListView xListView;
	private RelativeLayout rel;
	private Get_user_order_listRequest requestBean;
	private ArrayList<OrderBeanResponse> list;
	private PinJiaInterFace interFace;
	private int page;
	private int type;
	private LongRentOrderListAdapter adapter;
	private OrderBeanResponse bean;// 数据发生改变的bean
	String nowDate;

	public void setType(int type) {
		this.type = type;
	}


	public LongRentOrderFragment() {
	}

	@Override
	public int setContentView() {
		return R.layout.fragment_order;
	}

	@Override
	public void initView() {
		xListView = (XListView) findViewById(R.id.xListView);
		rel = (RelativeLayout) findViewById(R.id.rel);
	}

	@Override
	public void initData() {
		interFace = new PinJiaInterFace() {
			@Override
			public void setTextView(final View v, final int positon) {

				if (list.get(positon).order_status.equals("2")) {
					Intent intent = new Intent(MyAppcation.getMyAppcation(), LongReRentOrderActivity.class);
					intent.putExtra("order_id", list.get(positon).order_id);
					startActivity(intent);
				} else {
					bean = list.get(positon);
					Intent intent = new Intent(MyAppcation.getMyAppcation(), LongRentOrderCommentActivity.class);
					intent.putExtra("bean", list.get(positon));
					startActivity(intent);
				}
			}
		};
		requestBean = new Get_user_order_listRequest();
		requestBean.type = type;
		page = 1;
		requestBean.pagesize = CodeConstant.pageSize;
		list = new ArrayList<OrderBeanResponse>();
		adapter = new LongRentOrderListAdapter(list, getActivity(), R.layout.item_longrent_order);
		adapter.setIn(new ListViewOnclickInterFace() {

			@Override
			public Object DoSomeThing(Object data) {
				OrderBeanResponse t = (OrderBeanResponse) data;
				Intent intent = new Intent(MyAppcation.getMyAppcation(), LongRentOrderDetailsActivity.class);
				intent.putExtra("orderbean", t);
				intent.putExtra("nowDate", nowDate);
				startActivity(intent);
				bean = t;
				return null;
			}
		});
		adapter.setInterFace(interFace);
		xListView.setAdapter(adapter);
		xListView.setPullRefreshEnable(true);
		xListView.setPullLoadEnable(true);
		promptDialog.show();
		getData();
	}

	private void getData() {
		requestBean.page = page;
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				xListView.stopRefresh();
				xListView.stopLoadMore();
				promptDialog.dismiss();
				try {
					ResponseUserOrderList base = JSON.parseObject(response, ResponseUserOrderList.class);
					nowDate = base.nowDate;
					if (page == 1)
						list.clear();
					list.addAll(base.list);
					if (base.list == null || base.list.size() == 0) {
						xListView.setPullLoadEnable(false);
					}
					if (list.size() > 0) {
						xListView.setVisibility(View.VISIBLE);
						rel.setVisibility(View.GONE);
					} else {
						xListView.setVisibility(View.GONE);
						rel.setVisibility(View.VISIBLE);
					}
					adapter.notifyDataSetChanged();
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				promptDialog.dismiss();
				xListView.stopRefresh();
				xListView.stopLoadMore();
			}
		});

	}

	@Override
	public void initEvent() {
		xListView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				xListView.setPullLoadEnable(true);
				page = 1;
				getData();
			}

			@Override
			public void onLoadMore() {
				page++;
				getData();
			}
		});
	}

	/**
	 * 0续租支付成功后PayUitl,1评价完成后LongRentOrderCommentActivity页面调用
	 */
	@Override
	public Object dataChang(int flag, Object data) {

		switch (flag) {
		case Type.F_LongRentOrder.DEFAULT:
			page = 1;
			promptDialog.show();
			getData();
			break;
		case Type.F_LongRentOrder.COMMENT_CODE:
			if (bean != null) {
				bean.is_comment = "1";
				adapter.notifyDataSetChanged();
			}
			break;

		default:
			break;
		}

		return super.dataChang(flag, data);

	}
}
